package com.fuelindustries.ant.tasks;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.FileSet;

import com.fuelindustries.ant.FuelAntVariables;
import com.fuelindustries.ant.jsfl.CloseFlash;
import com.fuelindustries.ant.jsfl.DeleteASOFiles;
import com.fuelindustries.ant.utils.AntUtils;
import com.fuelindustries.ant.utils.FLPHelper;
import com.fuelindustries.ant.utils.FileUtils;
import com.fuelindustries.ant.utils.OSUtils;

public class Flash extends Task 
{
	
	private String __path = ""; //path to flash application
	private boolean __quit = false; //whether to quit flash after compiling
	private boolean __verbose = false; //verbose mode
	private String __version = "CS3"; //Flash version to use CS3 or CS4
	private boolean __closeDocs = false; //close document after compiling
	private boolean __deleteaso = false; //deletes aso files
	
	private List<Task> __tasks = new ArrayList<Task>();
	
	Vector<FileSet> filesets = new Vector<FileSet>();

	
	public void execute() throws BuildException
	{

		FuelAntVariables.BASE_DIR = AntUtils.getFolderLocation( this.getLocation() ).replace( "\\", "/");

		createDirectory();
		
		if( __deleteaso )
		{
			DeleteASOFiles d = new DeleteASOFiles();
			d.deleteasofiles(getFlashPath());
		}
		
		processFileset();
		
		for( Task t : __tasks )
		{
			try
			{
				
				if( t instanceof Flp )
				{
					Vector<Fla> flpfiles = FLPHelper.getFLPFiles( ((Flp) t) );
					for( int i=0; i<flpfiles.size(); i++ )
					{
						Fla fla = flpfiles.get( i );
						fla.execute();
					}
				}
				else
				{
					t.execute();
				}
			}
			catch( Exception e )
			{
				finish();
				throw new BuildException( e.getMessage() );
			}
			
		}
		
		finish();
	}
	
	private void finish()
	{
		if( __quit )
		{
			CloseFlash cf = new CloseFlash();
			cf.close( getFlashPath() );
		}
		
		cleanup();
	}
	
	private void processFileset()
	{
        DirectoryScanner ds;
        for (FileSet fileset : filesets) 
        {
	       	 ds = fileset.getDirectoryScanner(getProject());
	       	 File dir = ds.getBasedir();
	       	 String[] filesInSet = ds.getIncludedFiles();
	       	 for (String filename : filesInSet) 
	       	 {
	       		File file = new File(dir,filename);
	       		String ext = FileUtils.getFileExtension( file );
	       		if( ext.equals( "fla" ) )
	       		{
	       			Fla fla = new Fla();
	       			fla.setFile( file.getAbsolutePath() );
	       			fla.setBuild( FuelAntVariables.PUBLISH );
	       			add( fla );
	       		}
	       		else if( ext.equals( "flp" ) )
	       		{
	       			Flp flp = new Flp();
	       			flp.setFile(file.getAbsolutePath());
	       			flp.setBuild( FuelAntVariables.PUBLISH );
	       			add( flp );
	       		}
	       	 }
        }
	}
	
	private void createDirectory()
	{
		File dir = new File( FuelAntVariables.BASE_DIR + FuelAntVariables.TEMP_DIR );
		try
		{
			dir.mkdir();
		}
		catch( SecurityException e )
		{
			trace( e.getMessage() );
		}
	}
	
	private void cleanup()
	{
		File dir = new File( FuelAntVariables.BASE_DIR + FuelAntVariables.TEMP_DIR );
		boolean success = FileUtils.deleteDirRecursive(dir);
		if( !success )
		{
			trace( "couldn't delete temp folder" );
		}
	}
	
	public void add( Fla task )
	{
		task.setFlashPath( getFlashPath() );
		task.setClose( __closeDocs );
		task.setVerbose( __verbose );
		__tasks.add( task );
	}
	
	public void add( Flp task )
	{		
		task.setFlashPath( getFlashPath() );
		task.setVerbose( __verbose );
		__tasks.add( task );
	}
	
	public void setPath( String path )
	{
		__path = path;
	}
	
	public void setQuit( boolean quit )
	{
		__quit = quit;
	}
	
	public void setVerbose( boolean verbose )
	{
		__verbose = verbose;
	}
	
	public void setCloseDocs( boolean closedocs )
	{
		__closeDocs = closedocs;
	}
	
	public void setVersion( String version )
	{
		__version = version;
	}
	
	public void setDeleteaso( boolean deleteaso )
	{
		__deleteaso = deleteaso;
	}
	
	public String getFlashPath()
	{
		if( __path.length() == 0 )
		{			
			if( __version.equals( "CS3" ) )
			{
				if( OSUtils.isWindows() )
				{
					__path = "C:\\Program Files\\Adobe\\Adobe Flash CS3\\Flash.exe";
				}
				else if( OSUtils.isMac() )
				{
					__path = "Adobe Flash CS3";
				}
			}
			else if( __version.equals( "CS4" ) )
			{
				if( OSUtils.isWindows() )
				{
					__path = "C:\\Program Files\\Adobe\\Adobe Flash CS4\\Flash.exe";
				}
				else if( OSUtils.isMac() )
				{
					__path = "Adobe Flash CS4";
				}
			}
			else if( __version.equals( "CS5" ) )
			{
				if( OSUtils.isWindows() )
				{
					__path = "C:\\Program Files\\Adobe\\Adobe Flash CS5\\Flash.exe";
				}
				else if( OSUtils.isMac() )
				{
					__path = "Adobe Flash CS5";
				}
			}
		}
		
		return( __path );
	}
	
	 public void addFileSet(FileSet fileset) 
	 {
		 if (!filesets.contains(fileset)) {
	         filesets.add(fileset);
	      }

	 }
	
	public void trace( String msg )
	{
		System.out.println( msg );
	}
}



/*
Features

- list of movies to compile
- provide a file set 
- quit flash
- delete aso files
- compile project
- launch swf
- close all documents
- export swf //exports the fla to a specified source
- test swf //does a test movie and launches the swf in the IDE
- publish swf //publishes the fla with the current settings
- select Flash version
- verbose mode


<flash path="path to flash exe" quit="true|false" verbose="true|false" version="CS3|CS4" closeDocs="true|false" deleteaso="true|false">
	<fla file="path to the fla" build="export|publish|test" swf="if build == export path to swf" />	
	<fla dir="path to the dir, will publish all fla's in dir" build="publish" />
	<flp file="path to flp" build="publish|test" />
</flash>



*/